/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package qrlogin.test;

import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import qrlogin.QRLoginOnLoginCheckListener;
import qrlogin.QRLoginOnLoginListener;
import qrlogin.QRLoginOnLogoutListener;
import qrlogin.QRLoginServlet;

/**
 *
 * @author adee
 */
@WebServlet(name="QRTestServlet", urlPatterns={"/QRTestServlet.do"})
public class QRTestServlet extends QRLoginServlet {

    public QRTestServlet() {
          super();

          this.setLoginCheckListener(new QRLoginOnLoginCheckListener() {

            @Override
            public boolean isLogined(HttpServletRequest request) {
                User user = (User) request.getSession().getAttribute("user");
                if (user==null) user = new User();
                  request.getSession().setAttribute("user", user);
                return user.isLogined();
             }
         });

         this.setLoginListener(new QRLoginOnLoginListener() {

            @Override
            public boolean onLogin(HttpServletRequest request,List<String> errors) {
                User user = (User) request.getSession().getAttribute("user");
                if (user==null) user = new User();
                user.loginMe(request.getParameter("password"), request.getParameter("username"));

                
                Enumeration param = request.getParameterNames();
                while(param.hasMoreElements()){
                   String key = (String) param.nextElement();

                   System.out.println(key+" : "+request.getParameter(key));
                }
                 Date date = new Date();
                 System.out.println(date.toString()+":"+"onLogin");

                if (!user.isLogined()) errors.add("Rossz felhasználónév, vagy jelszó");

                request.getSession().setAttribute("user", user);
                
                return user.isLogined();
            }
        });

        this.setLogoutListener(new QRLoginOnLogoutListener() {

            @Override
            public boolean onLogout(HttpServletRequest request) {
                User user = (User) request.getSession().getAttribute("user");
                user.setLogined(false);
                request.getSession().setAttribute("user", user);
                return true;
            }
        });
        

        this.getServletParameters().setLoginLink("http://192.168.1.101:8080/QRLoginTest/QRTestServlet.do");
        this.getServletParameters().setSecuredPage("http://192.168.1.101:8080/QRLoginTest/");
        this.getServletParameters().setMainPage("http://192.168.1.101:8080/QRLoginTest/");
  

        this.getServletParameters().setLoginLink("http://ezhosting.hu:8080/QRLoginTest/QRTestServlet.do");
        this.getServletParameters().setSecuredPage("http://ezhosting.hu:8080/QRLoginTest/");
        this.getServletParameters().setMainPage("http://ezhosting.hu:8080/QRLoginTest/");
 
 
    }


    
}
